﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if WINDOWS
using System.Management;
#endif

namespace Brain
{
    public class ComputerInfo : IDisposable
    {
        public ProcessorInfo Processor;
        public RamInfo Ram;
        public string SystemName;

        public virtual void Dispose()
        {
            Processor = null;
            SystemName = null;
        }
        public virtual void Reset()
        {
        }

        public ComputerInfo()
        {
            this.Processor = GetProcessorInfo();
            this.Ram = GetRamInfo();
        }

        public RamInfo GetRamInfo()
        {

#if WINDOWS
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "select * from Win32_ComputerSystem");
            RamInfo p = new RamInfo();
            foreach (ManagementObject obj in searcher.Get())
            {
                PropertyDataCollection data = obj.Properties;
                p.SizeBytes = data["TotalPhysicalMemory"].Value.ToString();
                long bytes = long.Parse(p.SizeBytes);
                bytes /= 1024;
                bytes /= 1024;
                p.SizeMBytes = bytes.ToString();
            }
            return p;
#endif
#if XBOX
            RamInfo xboxRam = new RamInfo();
            xboxRam.SizeBytes = "536870912";
            xboxRam.SizeMBytes = "512";
            return xboxRam;
#endif
            return null;
        }

        public ProcessorInfo GetProcessorInfo()
        {
#if WINDOWS
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "select * from Win32_Processor");
            ProcessorInfo p = new ProcessorInfo();
            foreach (ManagementObject obj in searcher.Get())
            {
                PropertyDataCollection data = obj.Properties;
                p.Clock = data["CurrentClockSpeed"].Value.ToString();
                p.DataWidth = data["DataWidth"].Value.ToString();
                p.Name = data["Name"].Value.ToString();
                p.NumberOfCores = data["NumberOfCores"].Value.ToString();
                p.NumberOfLogicalProcessors = data["NumberOfLogicalProcessors"].Value.ToString();
                this.SystemName = data["SystemName"].Value.ToString();
            }
            return p;
#endif
#if XBOX
            ProcessorInfo xboxProc = new ProcessorInfo();
            xboxProc.Clock = "3200";
            xboxProc.DataWidth = "?";
            xboxProc.Name = "Xbox 360";
            xboxProc.NumberOfCores = "3";
            xboxProc.NumberOfLogicalProcessors = "3";
            return xboxProc;
#endif
            return null;
        }
    }
}
